package com.ccl.iot.template;import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ccl.iot.R;

public class DialogTitled extends StandardDialog{
	//private RelativeLayout iTitleBar = null;
	private LinearLayout iTitleBarLeft = null;
	private int iTitleBarMinChildCount = 0;
	private View iBack = null;
	private TextView iTitle = null;
	private LinearLayout iTitleBarRight = null;
	private LinearLayout iContentView = null;
	
	public DialogTitled(Context context) {
		super(context);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		super.requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.setContentView(R.layout.dialog_titled);
		
		//iTitleBar = (RelativeLayout) super.findViewById(R.id.iTitleBar);
		iTitleBarLeft = (LinearLayout) super.findViewById(R.id.iTitleBarLeft);
		if (iTitleBarLeft != null){
			iTitleBarMinChildCount = iTitleBarLeft.getChildCount();
			iBack = super.findViewById(R.id.iBack);
			handleBackEvent();
		}
		iTitle = (TextView) super.findViewById(R.id.iTitle);
		iTitleBarRight = (LinearLayout) super.findViewById(R.id.iTitleBarRight);
		
		iContentView = (LinearLayout) super.findViewById(R.id.iContentView);
	}
	
	public boolean setBackable(boolean aBackable){
		if (iBack != null){
			iBack.setVisibility(aBackable ? View.VISIBLE : View.GONE);
		}
		
		return false;
	}
	
	private void handleBackEvent(){
		if (iBack != null){
			iBack.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					cancel();
				}
			});
		}
	}

	@Override
	public void setContentView(View view) {
		if (iContentView != null){
			iContentView.addView(view);
		}
	}

	@Override
	public void setContentView(View view, LayoutParams params) {
		if (iContentView != null){
			iContentView.addView(view, params);
		}
	}
	
	@Override
	public void setContentView(int layoutResID) {
		if (iContentView != null){
			LayoutInflater tInflater = LayoutInflater.from(getContext());
			
			if (tInflater != null){
				View tView = tInflater.inflate(layoutResID, null);
				
				if (tView != null){
					iContentView.addView(tView);
				}
			}
		}else{
			super.setContentView(layoutResID);
		}
	}
	
	public View getContentView(){
		return iContentView;
	}

	@Override
	public void setTitle(CharSequence title) {
		if (iTitle != null){
			iTitle.setText(title);
		}
		
		super.setTitle(title);
	}

	@Override
	public void setTitle(int titleId) {
		setTitle(this.getContext().getResources().getText(titleId));
	}
	
	private void addChildView(ViewGroup aParent, View aChild){
		if (aParent != null && aChild != null){
			aParent.addView(aChild);
		}
	}
	
	private int getChildViewCount(ViewGroup aParent){
		if (aParent != null){
			return aParent.getChildCount();
		}else{
			return 0;
		}
	}
	
	private void addChildView(ViewGroup aParent, View aChild, int aIndex){
		if (aParent != null && aChild != null){
			aParent.addView(aChild, aIndex);
		}
	}
	
	private void removeChildViewAt(ViewGroup aParent, int aIndex){
		if (aParent != null){
			aParent.removeViewAt(aIndex);
		}
	}
	
	private void removeAllChildView(ViewGroup aParent){
		if (aParent != null){
			aParent.removeAllViews();
		}
	}
	
	public int getLeftTitleViewCount(){
		return getChildViewCount(iTitleBarLeft) - iTitleBarMinChildCount;
	}
	
	public void addLeftTitleView(View aView){
		addChildView(iTitleBarLeft, aView);
	}
	
	public void removeLeftTitleView(int aIndex){
		removeChildViewAt(iTitleBarLeft, aIndex);
	}
	
	public void removeAllLeftTitleView(){
		if (iTitleBarMinChildCount == 0){
			removeAllChildView(iTitleBarLeft);
		}else{
			if (iTitleBarLeft != null){
				for (int i = iTitleBarMinChildCount, tCount = iTitleBarLeft.getChildCount() - 1; i < tCount; i++){
					iTitleBarLeft.removeViewAt(i);
				}
			}
		}
	}
	
	public int getRightTitleViewCount(){
		return getChildViewCount(iTitleBarRight);
	}
	
	public void addRightTitleView(View aView){
		addChildView(iTitleBarRight, aView);
	}
	
	public void addRightTitleView(View aView, int aIndex){
		addChildView(iTitleBarRight, aView, aIndex);
	}
	
	public void removeRightTitleView(int aIndex){
		removeChildViewAt(iTitleBarRight, aIndex);
	}
	
	public void removeAllRightTitleView(){
		removeAllChildView(iTitleBarRight);
	}
	
}
